/*
 * Copyright (c) Koala 2013 All Rights Reserved
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.openkoala.koala.dd.ui;

import java.util.Map;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * 类    名：KeyValueDialog.java
 *   
 * 功能描述：KEY-VALUE配置对话框
 *  
 * 创建日期：2013-2-1上午11:17:38     
 * 
 * 版本信息：
 * 
 * 版权信息：Copyright (c) 2013 Koala All Rights Reserved
 * 
 * 作    者：lingen(lingen.liu@gmail.com)
 * 
 * 修改记录： 
 * 修 改 者    修改日期     文件版本   修改说明	
 */
public class KeyValueDialog extends Dialog {
    
    private Text value;

    private Text key;

    private String keyString;
    
    private String valueString;
    /**
     * @param parentShell
     */
    private KeyValueDialog(Shell parentShell) {
        super(parentShell);
    }
    
    
    protected void configureShell(Shell shell) {
        super.configureShell(shell);
        shell.setText("KEY-VALUE配置");
    }
    
    
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);
        initializeDialogUnits(composite);
        GridData mainGridData = new GridData(250, 100);
        composite.setLayoutData(mainGridData);
        composite.setLayout(new FormLayout());
        
        Label keyLabel = new Label(composite,SWT.NONE);
        keyLabel.setText("KEY值:");
        FormData layoutData = new FormData();
        layoutData.top = new FormAttachment(0,10);
        layoutData.left = new FormAttachment(0,20);
        layoutData.right = new FormAttachment(keyLabel,60,SWT.LEFT);
        layoutData.bottom = new FormAttachment(keyLabel,25,SWT.TOP);
        keyLabel.setLayoutData(layoutData);
        
        
        key = new Text(composite,SWT.BORDER);
        key.setText(keyString==null?"":keyString);
        layoutData = new FormData();
        layoutData.top = new FormAttachment(0,10);
        layoutData.left = new FormAttachment(keyLabel,20);
        layoutData.right = new FormAttachment(key,100,SWT.LEFT);
        layoutData.bottom = new FormAttachment(key,25,SWT.TOP);
        key.setLayoutData(layoutData);
        
        Label valueLabel = new Label(composite,SWT.NONE);
        valueLabel.setText("VALUE值:");
        layoutData = new FormData();
        layoutData.top = new FormAttachment(keyLabel,10);
        layoutData.left = new FormAttachment(0,20);
        layoutData.right = new FormAttachment(valueLabel,60,SWT.LEFT);
        layoutData.bottom = new FormAttachment(valueLabel,25,SWT.TOP);
        valueLabel.setLayoutData(layoutData);
        
        value = new Text(composite,SWT.BORDER);
        value.setText(valueString==null?"":valueString);
        layoutData = new FormData();
        layoutData.top = new FormAttachment(keyLabel,10);
        layoutData.left = new FormAttachment(valueLabel,20);
        layoutData.right = new FormAttachment(value,100,SWT.LEFT);
        layoutData.bottom = new FormAttachment(value,25,SWT.TOP);
        value.setLayoutData(layoutData);
        
        return parent;
    }
    
    public void refersh(String key,String value){
        this.keyString = key;
        this.valueString = value;
    }
    
    @Override
    protected void buttonPressed(int buttonId) {
        if(buttonId==0){
            keyString = this.key.getText();
            if(keyString==null || keyString.trim().equals("")){
                MessageDialog.openError(Display.getCurrent().getActiveShell(), "错误", "KEY值不能为空");
                return;
            }
            valueString = this.value.getText();
            if(valueString==null || valueString.equals("")){
                MessageDialog.openError(Display.getCurrent().getActiveShell(), "错误", "VALUE值不能为空");
                return;
            }
        }
        super.buttonPressed(buttonId);
    }

    public static KeyValueDialog getInstance(){
        KeyValueDialog dialog = new KeyValueDialog(Display.getCurrent().getActiveShell());
        return dialog;
    }
    
    public String getKey(){
        return keyString;
    }
    
    public String getValue(){
        return valueString;
    }
    
}
